//
//  Util.m
//  Hai
//
//  Created by wangzijian on 15/10/8.
//  Copyright (c) 2015年 wangzijian. All rights reserved.
//

#import "Util.h"
#import <Foundation/Foundation.h>

#import <CommonCrypto/CommonDigest.h>

#include <ifaddrs.h>
#include <arpa/inet.h>



@implementation Util

+ (NSString *)getSignature:(NSString *) date{
    if (date!=nil&&![date isEqual:[NSNull null]]) {
        date = [NSString stringWithFormat:@"%@",date];
        if (![date isEqualToString:@""]) {
            NSString *sign = [[[NSMutableDictionary alloc]initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"url" ofType:@"plist"]] objectForKey:@"URL_SIGN"];
            NSString *signature = [self getMd5_32Bit_String:[NSString stringWithFormat:@"%@%@",date,sign]];
            return signature;
        }else{
            return  @"";
        }
        
    }else{
        return  @"";
    }
    
}

+ (NSString *)getDate:(NSString *) date{
    if (date!=nil&&![date isEqual:[NSNull null]]&&![date isEqualToString:@""] && ![date isEqualToString:@"<null>"]) {
        date = [[[date stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] substringFromIndex:6]stringByReplacingOccurrencesOfString:@")/" withString:@""];
        NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
        [formatter setDateStyle:NSDateFormatterMediumStyle];
        [formatter setTimeStyle:NSDateFormatterShortStyle];
        [formatter setDateFormat:@"yyyy-MM-dd"];
        NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[date doubleValue]/1000];
        NSString *nowtimeStr = [formatter stringFromDate:confromTimesp];//
        return nowtimeStr;
    }else{
        return  @"";
    }
    
}
+ (NSString *)getDateTime:(NSString *) date{
    if (date!=nil&&![date isEqual:[NSNull null]]&&![date isEqualToString:@""]) {
        date = [[[date stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] substringFromIndex:6]stringByReplacingOccurrencesOfString:@")/" withString:@""];
        NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
        [formatter setDateStyle:NSDateFormatterMediumStyle];
        [formatter setTimeStyle:NSDateFormatterShortStyle];
        [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[date doubleValue]/1000];
        NSString *nowtimeStr = [formatter stringFromDate:confromTimesp];//
        return nowtimeStr;
    }else{
        return  @"";
    }
}


+(NSString *)createPostURL:(NSMutableDictionary *)params

{
    
    NSString *postString=@"";
    
    for(NSString *key in [params allKeys])
        
    {
        
        NSString *value=[params objectForKey:key];
        
        postString=[postString stringByAppendingFormat:@"%@=%@&",key,value];
        
    }
    
    if([postString length]>1)
        
    {
        
        postString=[postString substringToIndex:[postString length]-1];
        
    }
    
    return postString;
    
}



+(NSString *)getCurrentDate

{
    
    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init] ;
    
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    return [dateFormatter stringFromDate:[NSDate date]];
    
}







//通过区分字符串






//利用正则表达式验证

+(BOOL)isValidateEmail:(NSString *)email {
    
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    
    return [emailTest evaluateWithObject:email];
    
}



+(BOOL)isValidateString:(NSString *)myString

{
    
    NSCharacterSet *nameCharacters = [[NSCharacterSet characterSetWithCharactersInString:@"_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"] invertedSet];
    NSRange userNameRange = [myString rangeOfCharacterFromSet:nameCharacters];
    
    if (userNameRange.location != NSNotFound) {
        
        ////NSLog(@"包含特殊字符");
        
        return FALSE;
        
    }else{
        
        return TRUE;
        
    }
    
    
    
}

+ (NSString *)valueOfPlistKey:(NSString *)key fileName: (NSString *) name{
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:name ofType:@"plist"];
    NSMutableDictionary *plistData = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
    NSString *str = [plistData valueForKey:key];
    return str;
}

+ (NSString *)deviceIPAdress {
    NSString *address = @"";
    struct ifaddrs *interfaces = NULL;
    struct ifaddrs *temp_addr = NULL;
    int success = 0;
    
    success = getifaddrs(&interfaces);
    
    if (success == 0) { // 0 表示获取成功
        
        temp_addr = interfaces;
        while (temp_addr != NULL) {
            if( temp_addr->ifa_addr->sa_family == AF_INET) {
                // Check if interface is en0 which is the wifi connection on the iPhone
                if ([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
                    // Get NSString from C String
                    address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
                }
            }
            
            temp_addr = temp_addr->ifa_next;
        }
    }
    
    freeifaddrs(interfaces);
    
    //NSLog(@"手机的IP是：%@", address);
    return address;
}
+ (NSString *)getMd5_32Bit_String:(NSString *)str{
    const char *cStr = [str UTF8String];
    unsigned char result[16];
    CC_MD5(cStr, (int)strlen(cStr), result); // This is the md5 call
    return [NSString stringWithFormat:
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];
}
+ (BOOL)isMobileNumber:(NSString *)mobileNum
{
    NSString * MOBILE = @"1[0-9][0-9]\\d{8}$";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    if ([regextestmobile evaluateWithObject:mobileNum] == YES)
    {
        return YES;
    }
    else
    {
        return NO;
    }
}
+ (BOOL)isValidatePwd:(NSString *) textString
{
    NSString* number=@"^[0-9a-zA-Z]*$";
    NSPredicate *numberPre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",number];
    return [numberPre evaluateWithObject:textString];
}
+(UIImage *) getImageFromURL:(NSString *)fileURL {
    UIImage * result;
    
    NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
    
    result = [UIImage imageWithData:data];
    
    return result;
    
}
+ (NSString*)loadNDDate:(NSString *) textString
{
    NSUserDefaults* ud = [NSUserDefaults standardUserDefaults];
    NSString* text = [NSString stringWithFormat:@"%@",[ud objectForKey:textString]];
    //    LLLog(@"pswd = %@", pswd);
    if (text != nil &&
        ![text isEqualToString:@""] &&
        ![text isEqualToString:@"(null)"]) {
        
        return text;
    }
    
    return nil;
}
@end
